package com.jf.controller;

import com.alibaba.fastjson.JSON;
import com.google.code.kaptcha.Constants;
import com.jf.bean.Userinfo;
import com.jf.form.LoginForm;
import com.jf.service.UserinfoService;
import com.jf.service.impl.UserinfoServiceImpl;
import com.jf.util.MyConstants;
import com.jf.util.MyUtils;
import com.jf.util.ResultJson;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;

@WebServlet("/login")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher("/WEB-INF/pages/admin/index.jsp").forward(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("application/json;charset=utf-8");
        UserinfoService userinfoService = new UserinfoServiceImpl();

        BufferedReader in = new BufferedReader(new InputStreamReader(request.getInputStream(), StandardCharsets.UTF_8));
        String result = MyUtils.getRequestJson(in);
        LoginForm loginForm = JSON.parseObject(result,LoginForm.class);
        response.getWriter().println(JSON.toJSON(userinfoService.login(loginForm)));
    }
}
